package wangm.netty.test1;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * description
 *
 * @author zk
 * @since 2018/7/23
 */
public class HtmlInBoundHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("reading... will return html");
        StringBuilder headBuilder = new StringBuilder();
        headBuilder.append("HTTP/1.1 200 OK\n");
        headBuilder.append("Content-type: text/html;charset=utf-8\n");

        StringBuilder bodyBuilder = new StringBuilder();
        bodyBuilder.append("<html> <header></head><body>this is netty response content</body></html");

        headBuilder.append("Conetnt-Length: " + bodyBuilder.toString().length() + "\n");

        ByteBuf result = Unpooled.wrappedBuffer((headBuilder.toString() + bodyBuilder.toString()).getBytes());
        ctx.writeAndFlush(result);
    }
}
